﻿Public Partial Class Simulate_Booking
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Dim clsFc As New ClassFunction
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not (IsPostBack) Then
            Dim lbPage As Label = CType(Master.FindControl("lbPage"), Label)
            lbPage.Text = "SIMULATE BOOKING "
        End If
    End Sub

    Private Sub radioVehicle_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radioVehicle.SelectedIndexChanged
        If (radioVehicle.SelectedValue = "0") Then
            'In
            txtVessel.Text = txtVesselOUT.Text
            PanelVEHICLE_IN.Visible = True
            PanelVEHICLE_OUT.Visible = False
            lbFT1.Text = "FT"
            lbFT2.Text = "FT"
        Else
            'OUT
            txtVesselOUT.Text = txtVessel.Text
            PanelVEHICLE_IN.Visible = False
            PanelVEHICLE_OUT.Visible = True
            lbFT1.Text = "M"
            lbFT2.Text = "M"
        End If
    End Sub

    Private Sub btnSaveProduct_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSaveProduct.Click
        Dim dt As DataTable = Session("Product_popup")
        If (dt.Rows.Count > 0) Then
            If (txtid.Text.Trim() <> "") Then
                Dim name() As String = txtid.Text.Split("-")
                Dim soldto As String = txtid.Text.Split("-")(0) & "฿"
                For i As Integer = 1 To name.Length - 1
                    soldto &= name(i) & " "
                Next
                ViewState("OwnerColumn") = soldto
            Else
                ViewState("OwnerColumn") = "฿Sold To"
            End If
            If (dt.Rows(0).Item("Product_Event") & "" = "L") Or (dt.Rows(0).Item("Product_Event") & "" = "R") Then
                lbType.Text = "DISCHARGING PORT"
                gridProduct.Columns(8).HeaderText = "DISCHARGING PORT"
            Else
                lbType.Text = "LOADING PORT"
                gridProduct.Columns(8).HeaderText = "LOADING PORT"
            End If
        End If
        gridProduct.DataSource = dt
        gridProduct.DataBind()
    End Sub

    Private Sub gridProduct_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gridProduct.RowCommand
        Dim Index As Integer = Convert.ToInt32(e.CommandArgument)
        If e.CommandName = "cmddelete" Then
            Dim ds As DataTable = Session("Product_popup")
            ds.Rows(Index).Delete()
            ds.AcceptChanges()
            If (ds.Rows.Count = 0) Then
                ds = CreateDatasetProduct()
            End If
            Session("Product_popup") = ds
            gridProduct.PageIndex = 0
            gridProduct.DataSource = Session("Product_popup")
            gridProduct.DataBind()
        ElseIf e.CommandName = "cmdedit" Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " document.getElementById('ctl00_contentplaceholder1_ProductPopup').src = '../FormPopup/Pre_Product_popup.aspx';", True)
            Session("IndexProduct") = Index
            Session("PORT_PRODUCT") = radioPort.SelectedValue
            ModalPopupExtender5.Show()
        End If
    End Sub

    Private Sub gridProduct_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridProduct.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim ImgDelete As ImageButton = CType(e.Row.FindControl("imgdelete"), ImageButton)
            Dim ImgEdit As ImageButton = CType(e.Row.FindControl("imgedit"), ImageButton)
            If (gridProduct.DataKeys(e.Row.RowIndex).Values("Product_name") & "" = "") Then
                ImgDelete.Visible = False
                ImgEdit.Visible = False
            End If
            If (gridProduct.DataKeys(e.Row.RowIndex).Values("Product_Event") & "" = "L") Then
                e.Row.Cells(0).Text = "Load"
            ElseIf (gridProduct.DataKeys(e.Row.RowIndex).Values("Product_Event") & "" = "D") Then
                e.Row.Cells(0).Text = "Disch"
            ElseIf (gridProduct.DataKeys(e.Row.RowIndex).Values("Product_Event") & "" = "R") Then
                e.Row.Cells(0).Text = "Load(IRPC)"
            ElseIf (gridProduct.DataKeys(e.Row.RowIndex).Values("Product_Event") & "" = "I") Then
                e.Row.Cells(0).Text = "Disch(IRPC)"
            End If
            If (gridProduct.DataKeys(e.Row.RowIndex).Values("Group_no") & "" = "0000000000") Then
                e.Row.BackColor = Drawing.Color.Red
            End If
        End If
    End Sub

    Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Try
            If (radioPort.SelectedValue = "") Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการเลือกท่าเรือ !!');", True)
                Exit Sub
            End If
            If Not (txtVessel.Text.Trim() = "") Then
                Dim ds As DataTable = clsGet.Get_VEHICLE_DETAIL(txtVessel.Text.Split("-")(0))
                If (ds.Rows.Count > 0) Then
                    'พบเรือ
                    'Update Vessel
                    ds = clsGet.Get_VEHICLE_DETAIL(txtVessel.Text.Split("-")(0))
                    txtVessel.Enabled = False
                    txtid.Enabled = False
                    radioVehicle.Enabled = False
                    btnSearch.Visible = False
                    radioPort.Enabled = False
                    'Bind Data VEHICLE Detail
                    lbLOA.Text = (CDbl(ds.Rows(0).Item("VEH_LENGTH")) * 3.281).ToString("############0.00")
                    lbDWT.Text = ds.Rows(0).Item("VEH_MAXWGT_C").ToString().Trim()
                    lbDraft.Text = (CDbl(ds.Rows(0).Item("DRAFT")) * 3.281).ToString("############0.00")
                    lbNation.Text = ds.Rows(0).Item("REG_CNTRY")
                    If Not ((ds.Rows(0).Item("STCMNR") & "").ToString().Trim() = "") Then
                        Try
                            lbGRT.Text = ds.Rows(0).Item("STCMNR").ToString().Replace("GRT", "").Replace("=", "").Replace("MT", "")
                        Catch ex As Exception

                        End Try
                    End If
                    PanelVEHICLE_IN_DETAIL.Visible = True
                    panelProductDetail.Visible = True
                    Session("Product_popup") = CreateDatasetProduct()
                    gridProduct.DataSource = Session("Product_popup")
                    gridProduct.DataBind()
                Else
                    'ไม่พบเรือ
                    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ข้อมูลเรือไม่ถูกต้อง!!');", True)
                End If
            Else
                'ยังไม่ระบุข้อมูลเรือ
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุเรือ!!');", True)
            End If

        Catch ex As Exception
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ไม่สามารถทำการเชื่อมต่อ SAP ได้!!');", True)
        End Try
    End Sub

    Private Sub btnSearchOUT_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearchOUT.Click
        If Not (txtVesselOUT.Text.Trim() = "") Then
            If (txtGRT.Text.Trim() = "") Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ GRT!!');", True)
                Exit Sub
            End If
            If (txtDraft.Text.Trim() = "") Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ Draft!!');", True)
                Exit Sub
            End If
            If (txtDWT.Text.Trim() = "") Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ DWT!!');", True)
                Exit Sub
            End If
            If (txtLOA.Text.Trim() = "") Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ LOA!!');", True)
                Exit Sub
            End If
            txtVesselOUT.Enabled = False
            radioVehicle.Enabled = False
            txtLOA.Enabled = False
            txtDWT.Enabled = False
            txtDraft.Enabled = False
            txtGRT.Enabled = False
            txtid.Enabled = False
            ddlNation.Enabled = False
            btnSearchOUT.Visible = False
            radioPort.Enabled = False
            panelProductDetail.Visible = True
            Session("Product_popup") = CreateDatasetProduct()
            gridProduct.DataSource = Session("Product_popup")
            gridProduct.DataBind()
            '  LoadDefault_MAT(ddlNation.SelectedValue)
        Else
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุเรือ!!');", True)
        End If
    End Sub

    Public Function CreateDatasetProduct() As DataTable
        Dim ds As New DataSet
        Dim dt As New DataTable
        Dim dr As DataRow
        ds.Tables.Add(dt)
        dt.Columns.Add("Product_no", GetType(String))
        dt.Columns.Add("Product_name", GetType(String))
        dt.Columns.Add("Product_Amount", GetType(String))
        dt.Columns.Add("Product_Event", GetType(String))
        dt.Columns.Add("Product_Description", GetType(String))
        dt.Columns.Add("Product_Unit", GetType(String))
        dt.Columns.Add("Group_name", GetType(String))
        dt.Columns.Add("Group_no", GetType(String))
        dt.Columns.Add("Product_BUYER_no", GetType(String))
        dt.Columns.Add("Product_BUYER_name", GetType(String))
        dt.Columns.Add("Product_SELLER_no", GetType(String))
        dt.Columns.Add("Product_SELLER_name", GetType(String))
        dt.Columns.Add("LoadDestination", GetType(String))
        dt.Columns.Add("DischDestination", GetType(String))
        dt.Columns.Add("DestinationShow", GetType(String))
        dt.Columns.Add("Owner_Product_no", GetType(String))
        dt.Columns.Add("Owner_Product_name", GetType(String))
        dr = ds.Tables(0).NewRow
        dr.Item("Product_no") = ""
        dr.Item("Product_name") = ""
        dr.Item("Product_Amount") = ""
        dr.Item("Product_Event") = ""
        dr.Item("Product_Description") = ""
        dr.Item("Product_Unit") = ""
        dr.Item("Group_name") = ""
        dr.Item("Group_no") = ""
        dr.Item("Product_BUYER_no") = ""
        dr.Item("Product_BUYER_name") = ""
        dr.Item("Product_SELLER_no") = ""
        dr.Item("Product_SELLER_name") = ""
        dr.Item("LoadDestination") = ""
        dr.Item("DischDestination") = ""
        dr.Item("DestinationShow") = ""
        dr.Item("Owner_Product_no") = ""
        dr.Item("Owner_Product_name") = ""
        ds.Tables(0).Rows.Add(dr)
        ds.Tables(0).AcceptChanges()
        Return ds.Tables(0)
    End Function

    Private Sub btnProduct_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnProduct.Click
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " document.getElementById('ctl00_contentplaceholder1_ProductPopup').src = '../FormPopup/Pre_Product_popup.aspx';", True)
        Session("PORT_PRODUCT") = radioPort.SelectedValue
        ModalPopupExtender5.Show()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Response.Redirect("../Form/Simulate_Booking.aspx?P4=B", True)
    End Sub

    Private Sub btnCompute_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCompute.Click
        If Not (ComputeMAT()) Then
            Exit Sub
        End If
        Dim dsProduct As DataTable = Session("Product_popup")
        If (gridProduct.DataKeys(0).Values("Product_no") & "" <> "") Then
            If dsProduct Is Nothing Then
                Response.Redirect("../Form/Logon.aspx", True)
            End If
            If (dsProduct.Rows.Count = 0) Then
                Response.Redirect("../Form/Logon.aspx", True)
            End If
        End If

        Dim StandardTimePlus As String = "0"


        If (txtWater.Text.Trim() <> "0") Then
            Dim dst As DataSet = Session("MAT_USE")
            Dim cNew As Boolean = True
            For Each dr As DataRow In dst.Tables(0).Rows
                If (dr.Item("MATERIAL") = "WP") Then
                    dr.Item("Count") = txtWater.Text
                    dst.AcceptChanges()
                    cNew = False
                    Exit For
                End If
            Next
            If (cNew) Then
                Dim Plant As String = ""
                If (radioPort.SelectedValue = "L") Then
                    Plant = "1103"
                Else
                    Plant = "1104"
                End If
                Dim dtMat As DataSet = clsGet.Get_MeterailDefault("WP", False, "''", "", "", "", "", "", "", "", Plant)
                If (dtMat.Tables(0).Rows.Count > 0) Then
                    Dim dr As DataRow = dst.Tables(0).NewRow()
                    dr.Item("MATERIAL") = dtMat.Tables(0).Rows(0).Item("MATERIAL") & ""
                    dr.Item("Text_line") = dtMat.Tables(0).Rows(0).Item("Text_line") & ""
                    dr.Item("Unit") = dtMat.Tables(0).Rows(0).Item("Unit") & ""
                    dr.Item("Count") = txtWater.Text
                    dr.Item("MAT_Default") = "N"
                    dr.Item("KBETR") = dtMat.Tables(0).Rows(0).Item("KBETR") & ""
                    dr.Item("KONWA") = dtMat.Tables(0).Rows(0).Item("KONWA") & ""
                    dr.Item("MAT_Relation") = dtMat.Tables(0).Rows(0).Item("Mat_Relation") & ""
                    dr.Item("MAT_ComputeM") = "Y"
                    dst.Tables(0).Rows.Add(dr)
                    dst.AcceptChanges()
                    Session("MAT_USE") = dst
                Else
                    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ท่าเรือนี้ไม่ได้ทำการจำหน่ายน้ำ!!');", True)
                    Exit Sub
                End If
            End If


        End If
        If (txtElectricity.Text.Trim() <> "0") Then
            Dim dst As DataSet = Session("MAT_USE")
            Dim cNew As Boolean = True
            For Each dr As DataRow In dst.Tables(0).Rows
                If (dr.Item("MATERIAL") = "ELEC.POWER") Then
                    dr.Item("Count") = txtWater.Text
                    dst.AcceptChanges()
                    cNew = False
                    Exit For
                End If
            Next
            If (cNew) Then
                Dim Plant As String = ""
                If (radioPort.SelectedValue = "L") Then
                    Plant = "1103"
                Else
                    Plant = "1104"
                End If
                Dim dtMat As DataSet = clsGet.Get_MeterailDefault("ELEC.POWER", False, "''", "", "", "", "", "", "", "", Plant)
                If (dtMat.Tables(0).Rows.Count > 0) Then
                    Dim dr As DataRow = dst.Tables(0).NewRow()
                    dr.Item("MATERIAL") = dtMat.Tables(0).Rows(0).Item("MATERIAL") & ""
                    dr.Item("Text_line") = dtMat.Tables(0).Rows(0).Item("Text_line") & ""
                    dr.Item("Unit") = dtMat.Tables(0).Rows(0).Item("Unit") & ""
                    dr.Item("Count") = txtElectricity.Text
                    dr.Item("MAT_Default") = "N"
                    dr.Item("KBETR") = dtMat.Tables(0).Rows(0).Item("KBETR") & ""
                    dr.Item("KONWA") = dtMat.Tables(0).Rows(0).Item("KONWA") & ""
                    dr.Item("MAT_Relation") = dtMat.Tables(0).Rows(0).Item("Mat_Relation") & ""
                    dr.Item("MAT_ComputeM") = "Y"
                    dst.Tables(0).Rows.Add(dr)
                    dst.AcceptChanges()
                    Session("MAT_USE") = dst
                Else
                    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ท่าเรือนี้ไม่ได้ทำการจำหน่ายไฟฟ้า!');", True)
                    Exit Sub
                End If
            End If
        End If
        'If (chkShifting.Checked) Then
        '    Dim Plant As String = ""
        '    If (radioPort.SelectedValue = "L") Then
        '        Plant = "1103"
        '    Else
        '        Plant = "1104"
        '    End If
        '    Dim MatShifting As DataTable = clsGet.Get_Meterial_Shifting(Plant)
        '    Dim dst As DataSet = Session("MAT_USE")
        '    Dim cNew As Boolean = True
        '    For Each dsf As DataRow In MatShifting.Rows
        '        For Each dr As DataRow In dst.Tables(0).Rows
        '            If (dr.Item("MATERIAL") = dsf.Item("MATERIAL")) Then
        '                cNew = False
        '                Exit For
        '            End If
        '        Next
        '        If (cNew) Then
        '            Dim dr As DataRow = dst.Tables(0).NewRow()
        '            dr.Item("MATERIAL") = dst.Tables(0).Rows(0).Item("MATERIAL") & ""
        '            dr.Item("Text_line") = dst.Tables(0).Rows(0).Item("Text_line") & ""
        '            dr.Item("Unit") = dst.Tables(0).Rows(0).Item("Unit") & ""
        '            dr.Item("Count") = "1"
        '            dr.Item("MAT_Default") = "N"
        '            dr.Item("KBETR") = dst.Tables(0).Rows(0).Item("KBETR") & ""
        '            dr.Item("KONWA") = dst.Tables(0).Rows(0).Item("KONWA") & ""
        '            dr.Item("MAT_Relation") = dst.Tables(0).Rows(0).Item("Mat_Relation") & ""
        '            dr.Item("MAT_ComputeM") = "Y"
        '            dst.Tables(0).Rows.Add(dr)
        '            dst.AcceptChanges()
        '            Session("MAT_USE") = dst
        '        End If

        '    Next

        'End If


        Session("StandardTimePlus") = CInt(StandardTimePlus)
        ' Dim dsProduct As DataTable = Session("Product_popup")
        Dim ProductName As String = ""
        Dim ProductAmount As Double = 0.0
        Dim ProductAmountByLine As String = ""
        Dim ProductAmountByProduct As String = ""
        Dim ProductType As String = ""
        Dim ProductBuyer As String = ""
        Dim ProductSeller As String = ""
        For Each dr As DataRow In dsProduct.Rows
            If (dr.Item("Product_name") & "" <> "") Then
                ProductName &= "," & dr.Item("Product_name")
                ProductAmountByLine &= "," & dr.Item("Product_Amount")
                ProductAmount = ProductAmount + (CDbl(dr.Item("Product_Amount")))
                ProductType &= "," & dr.Item("Product_Unit")
                ProductBuyer &= "," & dr.Item("Product_BUYER_name")
                ProductSeller &= "," & dr.Item("Product_SELLER_name")
                ProductAmountByProduct &= dr.Item("Product_name") & "฿" & dr.Item("Product_Amount") & "฿"
            End If
        Next
        If (ProductName.Length > 0) Then
            ProductName = ProductName.Substring(1, ProductName.Length - 1)
            ProductAmountByLine = ProductAmountByLine.Substring(1, ProductAmountByLine.Length - 1)
            ProductType = ProductType.Substring(1, ProductType.Length - 1)
            ProductBuyer = ProductBuyer.Substring(1, ProductBuyer.Length - 1)
            ProductSeller = ProductSeller.Substring(1, ProductSeller.Length - 1)
        End If

        ViewState("Product_NameAmount") = ProductName & "$" & ProductAmount & "$" & ProductType & "$" & ProductBuyer & "$" & ProductSeller
        If (radioVehicle.SelectedValue = "0") Then
            Dim ds As DataTable = clsGet.Get_VEHICLE_DETAIL(txtVessel.Text.Split("-")(0))
            If (ds.Rows.Count > 0) Then
                Dim NameAgent As String = ""
                Dim CodeAgent As String = ""
                If (txtid.Text.Trim() <> "") Then
                    Dim AgentLoops() As String = txtid.Text.Split("-")
                    For i As Integer = 0 To AgentLoops.Length - 1
                        If (i <> 0) Then
                            NameAgent &= "-" & AgentLoops(i).ToString()
                        End If
                    Next
                    CodeAgent = txtid.Text.Split("-")(0)
                    NameAgent = txtid.Text.Split("-")(0) & "-" & NameAgent.Substring(1, NameAgent.Length - 1)
                End If
                Dim tVessel As String = ""
                Dim AgentLoop() As String = txtVessel.Text.Split("-")
                For i As Integer = 0 To AgentLoop.Length - 1
                    If (i <> 0) Then
                        tVessel &= "-" & AgentLoop(i).ToString()
                    End If
                Next
                Dim Nation As String = ""
                If lbNation.Text = "TH" Then
                    Nation = "T"
                Else
                    Nation = "O"
                End If
                tVessel = tVessel.Substring(1, tVessel.Length - 1)
                Session("Value") = NameAgent & "$" & tVessel & "$" & ProductName & "$" & lbDWT.Text & "$" & lbLOA.Text & "$" & ProductAmount & "$" & lbDraft.Text & "$Draft$" & raioType.SelectedValue & "$" & radioTypeBuy.SelectedValue & "$" & Nation & "$" & lbGRT.Text & "$" & radioPort.SelectedValue & "$" & ProductAmountByProduct & "$" & CodeAgent
                '  Session("Value") = lbLOA.Text & "$" & lbDWT.Text & "$" & lbDraft.Text & "$" & lbGRT.Text & "$" & tVessel & "$" & NameAgent & "$Draft" & "$" & ProductName & "$" & ProductAmount & "$" & ProductAmountByProduct & "$" & lbNation.Text & "$" & raioType.SelectedValue & "$" & ViewState("Status")
            Else
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ข้อมูลเรือไม่ถูกต้อง!!');", True)
                Exit Sub
            End If
        Else
            Dim nation As String = "XX"
            nation = ddlNation.SelectedValue
            Dim NameAgent As String = ""
            Dim CodeAgent As String = ""
            If (txtid.Text.Trim() <> "") Then
                Dim AgentLoops() As String = txtid.Text.Split("-")
                For i As Integer = 0 To AgentLoops.Length - 1
                    If (i <> 0) Then
                        NameAgent &= "-" & AgentLoops(i).ToString()
                    End If
                Next
                CodeAgent = txtid.Text.Split("-")(0)
                NameAgent = NameAgent.Substring(1, NameAgent.Length - 1)
            End If
            Session("Value") = NameAgent & "$" & txtVesselOUT.Text & "$" & ProductName & "$" & txtDWT.Text & "$" & txtLOA.Text & "$" & ProductAmount & "$" & txtDraft.Text & "$Draft$" & raioType.SelectedValue & "$" & radioTypeBuy.SelectedValue & "$" & nation & "$" & txtGRT.Text & "$" & radioPort.SelectedValue & "$" & ProductAmountByProduct & "$" & CodeAgent
            ' Session("Value") = txtLOA.Text & "$" & txtDWT.Text & "$" & txtDraft.Text & "$" & txtGRT.Text & "$" & txtVesselOUT.Text & "$" & NameAgent & "$Draft" & "$" & ProductName & "$" & ProductAmount & "$" & ProductAmountByProduct & "$" & nation & "$" & raioType.SelectedValue & "$" & ViewState("Status")
        End If
        'If (chkShifting.Checked) Then
        '    Session("Shifting") = "Y"
        'Else
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " document.getElementById('ctl00_contentplaceholder1_framePopup2').src = '../FormPopup/estimate.aspx?Simulate=Y';", True)
        Session("Shifting") = Nothing
        'End If
        ModalPopupExtender2.Show()
    End Sub

    Private Function ComputeMAT() As Boolean
        Dim National As String = ""
        Dim TypeBuy As String = ""
        If (radioVehicle.SelectedValue = "I") Then
            National = "T"
        Else
            National = "O"
            TypeBuy = radioTypeBuy.SelectedValue
        End If
        Dim Load As String = ""
        Dim Disch As String = ""
        Dim MaterailB As String = ""
        Dim VesselNation As String = ""
        Dim dt As DataTable = Session("Product_popup")
        For Each dr As DataRow In dt.Rows
            Dim sLoad As String = ""
            Dim sDisch As String = ""
            Dim sLoadIRPC As String = ""
            Dim sDischIRPC As String = ""
            Select Case dr.Item("Product_Event") & ""
                Case "L" : Load = "Y"
                    sLoad = "Y"
                Case "D" : Disch = "Y"
                    sDisch = "Y"
            End Select

            Dim dtMat As DataTable = clsGet.Get_Meterail_ProductMaterial(dr.Item("Product_no"), sLoad, sDisch, sLoadIRPC, sDischIRPC)
            For Each drMat As DataRow In dtMat.Rows
                If (drMat.Item("Material") <> "") Then
                    MaterailB = ",'" & drMat.Item("Material") & "'"
                End If
            Next
        Next
        If (radioVehicle.SelectedValue = "0") Then
            If lbNation.Text = "TH" Then
                VesselNation = "T"
            Else
                VesselNation = "O"
            End If
        Else
            VesselNation = ddlNation.SelectedValue
        End If
        LoadDefault_MAT(National, TypeBuy, Load, Disch, MaterailB, VesselNation)

        Return True
    End Function

    Private Sub LoadDefault_MAT(ByVal National As String, ByVal TypeBuy As String, ByVal Load As String, ByVal Disch As String, ByVal MaterialB As String, ByVal VesselNational As String)
        Session("MAT_USE") = clsGet.Get_MeterailDefault("", True, "", radioPort.SelectedValue, National, TypeBuy, Load, Disch, MaterialB, VesselNational, "")
    End Sub

    Private Sub raioType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles raioType.SelectedIndexChanged
        If raioType.SelectedValue = "O" Then
            rowTypeBuy.Visible = True
        Else
            rowTypeBuy.Visible = False
        End If
    End Sub


    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "M")
        End If
    End Sub
End Class